package com.jiayang.hhotel.presenter;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.PayMethodAdapter;
import com.jiayang.hhotel.bean.CheckOutMethodModel;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.PrePayModel;
import com.jiayang.hhotel.net.Api;
import com.jiayang.hhotel.ui.PrePayActivity;
import com.jiayang.hhotel.util.CommonUtil;
import com.jiayang.hhotel.util.LogUtil;
import com.jiayang.hhotel.util.ToastUtil;

import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;

/**
 * Created by Administrator on 2017/5/25 0025.
 * 预付逻辑类
 */

public class PrePayPresenter extends XPresent<PrePayActivity> {

    /**
     * 获取预付列表
     * @param mpLanguage 语言
     * @param mpOperation 状态0-当前；1-历史
     * @param mpGstAcct 客人帐号 580095
     * @param mpCronum 订单号
     * @param mpRegister 房间号
     * @param mpType 1-预订保证金、2-现住预付、3-取消预订、4-失约、5-提前离店
     */
    public void getPrePayList(int mpLanguage,int mpOperation,long mpGstAcct,long mpCronum,String mpRegister,long mpType){
        LogUtil.showLog("mpacct="+mpGstAcct+";cronum="+mpCronum+";mpRegister="+mpRegister+";type="+mpType);
        Api.getHomeSer().getPrePayList(Constant.PHONE,mpLanguage,mpOperation,mpGstAcct,mpCronum,mpRegister,mpType)
                .compose(XApi.<PrePayModel>getApiTransformer())
                .compose(XApi.<PrePayModel>getScheduler())
                .compose(getV().<PrePayModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<PrePayModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        CommonUtil.dismissProgressDialog(getV());
                        getV().sealPic(error.getType());
                    }

                    @Override
                    public void onNext(PrePayModel Model) {
                        LogUtil.showLog("预付请求="+Model.toString());
                        switch (Model.getState()){
                            case 9://请求成功
                                getV().setAdapter(Model.getPDepositListDto());
                                break;
                            case -1://失败
                                getV().sealPic(-1);
                                break;
                            case 1002://没有数据
                                getV().sealPic(1002);
                                break;
                            default:
                                ToastUtil.Toast(getV().getString(R.string.error_code)+Model.getState()+getV().getString(R.string.space)+getV().getString(R.string.service_error_contract_manager),getV());
                                break;
                        }
                    }
                });
    }

    /**
     * 获取结账方式列表
     * @param mpLanguage 语言
     * @param mpType 3
     * @param mpRegister 结账付款金额<0时使用(默认-1）
     * @param mpGstAcct 结账付款金额<0时使用(默认-1）
     * @param mpMark 结账付款金额<0时使用(默认-1）0-自付结账、1-公付结账
     * @param mpAmount 金额
     */
    public void asyncCheckOutMethodListLoad(int mpLanguage,int mpType,long mpRegister,long mpGstAcct,int mpMark,double mpAmount){
        Api.getHomeSer().checkOutMethod(Constant.PHONE,mpLanguage,mpType,mpRegister,mpGstAcct,mpMark,mpAmount)
                .compose(XApi.<CheckOutMethodModel>getApiTransformer())
                .compose(XApi.<CheckOutMethodModel>getScheduler())
                .compose(getV().<CheckOutMethodModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<CheckOutMethodModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        CommonUtil.dismissProgressDialog(getV());
                        ToastUtil.Toast(getV().getString(R.string.net_error),getV());
                    }

                    @Override
                    public void onNext(CheckOutMethodModel Model) {
                        CommonUtil.dismissProgressDialog(getV());
                        switch (Model.getState()){
                            case 9://成功
                                getV().initPayMethodCreate(Model.getPCashierPayListDtos());
                                break;
                            default:
                                getV().sealPic(-1);
                                ToastUtil.Toast(getV().getString(R.string.error_code)+Model.getState()+getV().getString(R.string.space)+getV().getString(R.string.service_error_contract_manager),getV());
                                break;
                        }
                    }
                });
    }

    private AlertDialog checkOutMethodDialog;
    /**
     * 选择支付方式对话框
     * @param context 上下文
     * @param list 集合
     */
    public void checkOutMethodDialog(Context context, List<CheckOutMethodModel.PCashierPayListDtosBean> list){
        final AlertDialog.Builder builder=new AlertDialog.Builder(context);
        View itemView= LayoutInflater.from(context).inflate(R.layout.dialog_pay_method,null);
        TextView textView= (TextView) itemView.findViewById(R.id.tv_title);
        Button enterMine= (Button) itemView.findViewById(R.id.bt_determine);
        textView.setText(context.getString(R.string.zhifu_method));
        ListView listView = (ListView) itemView.findViewById(R.id.lv);
        final PayMethodAdapter adapter=new PayMethodAdapter(context,list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setIndex(position);
            }
        });
        enterMine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                adapter.getIndex();
                checkOutMethodDialog.dismiss();
            }
        });
        builder.setView(itemView);
        checkOutMethodDialog = builder.show();
    }

}
